
***********************************************************************************************;
*   Code Name: Part3CreateHHVariables.sas
  	Authors: Per Siden and Thomas Jansson (Sveriges Riksbank)
	Created: February 8, 2013
	Last edited: April 13, 2015
***********************************************************************************************;

* This is the third step in a series of codes used in this project.
  The original thought is to run the codes in the following order:
	1. Part1InitializeLinda.sas
	2. Part2IncludeAssets.sas
	3. Part3CreateHHVariables.sas
	4. Part4RegionalImmigrantRatio.sas

 * In this file we create household variables by summing over the members of every household.  
   After this we have the final LINDA main files and we save those to the main directory.
***********************************************************************************************;

DM 'CLEAR LOG';
DM 'CLEAR OUTPUT';
Proc datasets LIB = work KILL;

***********************************************************************************************;
%let wvars = house vacationhouse farms multifamilyhouse industryhouse 
			 foreignhouse landvalue coop realassets bank
			 premiumbonds othersecurities insurance bonds mutualinterest
			 mutualequity mutualmixed mutualother stocks debtstudies 
			 debt otherassets fanobank risky riskfree
			 stocksmutual finw totalassets netw;
%let hhwvars = hhhouse hhvacationhouse hhfarms hhmultifamilyhouse hhindustryhouse 
			   hhforeignhouse hhlandvalue hhcoop hhrealassets hhbank
			   hhpremiumbonds hhothersecurities hhinsurance hhbonds hhmutualinterest
			   hhmutualequity hhmutualmixed hhmutualother hhstocks hhdebtstudies 
			   hhdebt hhotherassets hhfanobank hhrisky hhriskfree
			   hhstocksmutual hhfinw hhtotalassets hhnetw;
%let Nw = 29;
***********************************************************************************************;

* Count the number of household members with certain properties 
  and sum up wealth variables to household level;
%macro m3b(f_or_i);

%do y = 1999 %to 2007;

proc sort data = temp.lindamerged&f_or_i&y;
	by idhh inc;
run;

data temp.lindamerged&f_or_i&y;
	set temp.lindamerged&f_or_i&y;
	by idhh;
	if last.idhh then head = 1;
	else head = 0;
run;

data temp.lindamergedB&f_or_i&y;
	set temp.lindamerged&f_or_i&y;
	by idhh;
	retain hh_deceased hh_emigrated hh_children &hhwvars 0;
	if first.idhh then do;
		hh_deceased = 0;					*number of deceased household members;
		hh_emigrated = 0;					*number of emigrated household members;
		hh_children = 0;					*number of children in the household;
		%do k = 1 %to &Nw;
			%scan(&hhwvars,&k) = 0;
		%end;
	end;
	
	hh_deceased + deceased;
	if emigrationdate > 0 then hh_emigrated + 1;
	if age < 18 then do;
		hh_children + 1;									
	end;

	%do k = 1 %to &Nw;
		%scan(&hhwvars,&k) = %scan(&hhwvars,&k) + %scan(&wvars,&k);
	%end;
	if last.idhh then do;
		output;
	end;
	drop id;
run;

data temp.lindamergedC&f_or_i&y;
	merge temp.lindamerged&f_or_i&y 
		  temp.lindamergedB&f_or_i&y(keep = idhh hh_deceased hh_emigrated hh_children &hhwvars);
	by idhh;
	hh_adults = hh_size - hh_children; 		*number of adults in the household;
run;

%end;

%mend;
%m3b(f);
%m3b(i);

***********************************************************************************************;
* Save to main folder;
%macro m3c(f_or_i);

%do y = 1999 %to 2007;

data main.linda&f_or_i&y;
	set temp.lindamergedC&f_or_i&y;
run;

%end;

%mend;
%m3c(f);
%m3c(i);
***********************************************************************************************;
